package cn.nawang.ebeim.server.constants;

import cn.nawang.ebeim.server.util.SystemUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName: Config
 * @Description: TODO
 * @author 许炜毅
 * @date 2015年8月18日 上午10:21:15
 * @version v1.0
 */
public class Config {
  // 临时文件前缀
  public static final String TEMP_FILE_PREFIX = "temp_file_";
  // 临时数据源文件夹名称
  public static final String TEMP_DS_FOLDER = "temp_ds_folder";
  // 断点续传文件前缀
  public static final String BREAK_POINT_PREFIX = "break_point_";
  public static final String UPLOAD_FILE_PREFIX = "upload_file_";

  /** 版本树上传路径 **/
  public static final String VERSION_UPLOAD = "/version/upload";
  /** 版本树下载路径 **/
  public static final String VERSION_DOWNLOAD = "/version/download";
  /** 文件上传路径 **/
  public static final String FILE_UPLOAD = "/file/upload";
  /** 文件下载路径 **/
  public static final String FILE_DOWNLOAD = "/file/download";

  /** 文件在本地存储的存放目录 **/
  public static final String FILE_SAVE_DIR = "/ebeim/data/file/upload/";
  /** 版本树在本地存储的存放目录 **/
  public static final String VERSION_SAVE_DIR = "/ebeim/data/version/upload/";


  /** 文件/版本树上传以及下载根路径 **/
  public static String WORKING_DIR = null;

  public static String LOCAL_SAVE_DIR = null;

  /** 存储云连接配置 **/
  public static String STORAGE_SERVER_URL = null;
  public static String STORAGE_SERVER_USER_NAME = null;
  public static String STORAGE_SERVER_URL_PARAM = null;

  /** HDFS连接配置 **/
  public static String HADOOP_USER_NAME = null;
  public static String HADOOP_URL_PARAM = null;
  public static String HADOOP_URL = null;
  public static boolean outLog = true;
  public static int SESSION_TIME = 0;
  public static String ZK_HOSTS = null;
  public static String ZK_GROUP_ROOT = null;
  public static String STORAGE_UTIL_TYPE = null;
  public static Properties prop = null;

  /** 调用api路径 **/
  public static String API_URL = null;

  /** 是否启用文件迁移 **/
  public static String IS_OPEN_DATA_TRANSFER = null;

  /** zk最大重连次数 **/
  public static String MAX_SERVER_RETRIES = null;
  /** zk重连时间间隔 **/
  public static String SERVER_RETRIES_SLEEP = null;
  public static String STORAGE_CLOUD_NAME = null;

  /** 上传文件端口号 **/
  public static Integer UPLOAD_PORT = null;
  /** 上传文件主机地址(外网地址) **/
  public static String UPLOAD_HOST = null;
  /** 下载文件端口号 **/
  public static Integer DOWN_PORT = null;
  /** Socket监听端口 **/
  public static Integer SOCKET_PORT = null;
  /** 设置线程中队列的大小 **/
  public static Integer QUEUE_SIZE = null;
  /** 设置线程池大小 **/
  public static Integer THREAD_SIZE = null;

  /** 扫描文件夹间隔 **/
  public static Integer SCAN_INTERVAL = null;

  /** swift连接信息 **/
  public static String SWIFT_PROVIDER = null;
  public static String SWIFT_IDENTITY = null;
  public static String SWIFT_CREDENTIAL = null;
  public static String SWIFT_ENDPOINT = null;

  public static String SERVER_HOST = null;

  /** 写限速 **/
  public static Long READ_LIMIT = null;
  /** 读限速 **/
  public static Long WRITE_LIMIT = null;
  /** 周期 **/
  public static Long CHECK_INTERVAL = null;

  static {
    try {
      prop = null;
      if (prop == null) {
        init();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public static void init() {
    prop = new Properties();
    try {
      String confPath = SystemUtil.getConfPath() + "/config.properties"; // 从目录“程序目录/conf”下加载配置文件
      InputStream in = new FileInputStream(new File(confPath));
      prop.load(in);
      // prop.load(Class.class.getClass().getResourceAsStream("/resources/config.properties"));
      String log = prop.getProperty("outLog");
      HADOOP_USER_NAME = prop.getProperty("hadoop_user_name");
      HADOOP_URL_PARAM = prop.getProperty("hadoop_url_param");
      HADOOP_URL = prop.getProperty("hadoop_url");
      STORAGE_SERVER_USER_NAME = prop.getProperty("storage_server_user_name");
      STORAGE_SERVER_URL_PARAM = prop.getProperty("storage_sever_url_param");
      STORAGE_SERVER_URL = prop.getProperty("storage_server_url");
      API_URL = prop.getProperty("api_url");
      SESSION_TIME = Integer.valueOf(prop.getProperty("session_time"));
      ZK_HOSTS = prop.getProperty("zk_hosts");
      ZK_GROUP_ROOT = prop.getProperty("zk_group_root");
      STORAGE_UTIL_TYPE = prop.getProperty("storage_util_type");
      MAX_SERVER_RETRIES = prop.getProperty("server.retries.max");
      SERVER_RETRIES_SLEEP = prop.getProperty("server.retries.sleep");
      IS_OPEN_DATA_TRANSFER = prop.getProperty("is_open_data_transfer");
      STORAGE_CLOUD_NAME = prop.getProperty("storage_cloud_name");
      SWIFT_PROVIDER = prop.getProperty("swift_provider");
      SWIFT_IDENTITY = prop.getProperty("swift_identity");
      SWIFT_CREDENTIAL = prop.getProperty("swift_credential");
      SWIFT_ENDPOINT = prop.getProperty("swift_endpoint");

      UPLOAD_PORT = Integer.valueOf(prop.getProperty("upload_port"));
      try{
        UPLOAD_HOST = prop.getProperty("upload_host");
        if(UPLOAD_HOST == null || "".equals(UPLOAD_HOST.trim())){
          UPLOAD_HOST = SystemUtil.getLocalHostIp();
        }
      }catch (Exception e){
        UPLOAD_HOST = SystemUtil.getLocalHostIp();
      }
      DOWN_PORT = Integer.valueOf(prop.getProperty("down_port"));
      SOCKET_PORT = Integer.valueOf(prop.getProperty("socket_port"));
      QUEUE_SIZE = Integer.valueOf(prop.getProperty("queue_size"));
      THREAD_SIZE = Integer.valueOf(prop.getProperty("thread_size"));
      SCAN_INTERVAL = Integer.valueOf(prop.getProperty("scan_interval"));

      WORKING_DIR = prop.getProperty("working_dir");
      LOCAL_SAVE_DIR = prop.getProperty("local_save_dir");

      READ_LIMIT = Long.valueOf(prop.getProperty("write_limit"));
      WRITE_LIMIT = Long.valueOf(prop.getProperty("read_limit"));
      CHECK_INTERVAL = Long.valueOf(prop.getProperty("check_Interval"));

      SERVER_HOST = prop.getProperty("server.host");

      if ("0".equals(log)) {
        outLog = false;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Config.init();
    System.out.println(Config.API_URL);
  }

}
